3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 class PhpHttpRequest
extends MWHttpRequest
{
23 private $fopenErrors = [];
29 protected function urlToTcp( $url ) {
30 $parsedUrl = parse_url( $url );
32 return 'tcp://' . $parsedUrl['host'] . ':' . $parsedUrl['port'];
36 * Returns an array with a 'capath' or 'cafile' key
37 * that is suitable to be merged into the 'ssl' sub-array of
38 * a stream context options array.
39 * Uses the 'caInfo' option of the class if it is provided, otherwise uses the system
40 * default CA bundle if PHP supports that, or searches a few standard locations.
42 * @throws DomainException
44 protected function getCertOptions() {
47 if ( $this->caInfo
) {
48 $certLocations = [ 'manual' => $this->caInfo
];
51 foreach ( $certLocations as $key => $cert ) {
52 if ( is_dir( $cert ) ) {
53 $certOptions['capath'] = $cert;
55 } elseif ( is_file( $cert ) ) {
56 $certOptions['cafile'] = $cert;
58 } elseif ( $key === 'manual' ) {
59 // fail more loudly if a cert path was manually configured and it is not valid
60 throw new DomainException( "Invalid CA info passed: $cert" );
68 * Custom error handler for dealing with fopen() errors.
69 * fopen() tends to fire multiple errors in succession, and the last one
70 * is completely useless (something like "fopen: failed to open stream")
71 * so normal methods of handling errors programmatically
72 * like get_last_error() don't work.
75 * @param string $errstr
77 public function errorHandler( $errno, $errstr ) {
78 $n = count( $this->fopenErrors
) +
1;
79 $this->fopenErrors +
= [ "errno$n" => $errno, "errstr$n" => $errstr ];
83 * @see MWHttpRequest::execute
87 public function execute() {
90 if ( is_array( $this->postData
) ) {
91 $this->postData
= wfArrayToCgi( $this->postData
);
94 if ( $this->parsedUrl
['scheme'] != 'http'
95 && $this->parsedUrl
['scheme'] != 'https' ) {
96 $this->status
->fatal( 'http-invalid-scheme', $this->parsedUrl
['scheme'] );
99 $this->reqHeaders
['Accept'] = "*/*";
100 $this->reqHeaders
['Connection'] = 'Close';
101 if ( $this->method
== 'POST' ) {
102 // Required for HTTP 1.0 POSTs
103 $this->reqHeaders
['Content-Length'] = strlen( $this->postData
);
104 if ( !isset( $this->reqHeaders
['Content-Type'] ) ) {
105 $this->reqHeaders
['Content-Type'] = "application/x-www-form-urlencoded";
109 // Set up PHP stream context
112 'method' => $this->method
,
113 'header' => implode( "\r\n", $this->getHeaderList() ),
114 'protocol_version' => '1.1',
115 'max_redirects' => $this->followRedirects ?
$this->maxRedirects
: 0,
116 'ignore_errors' => true,
117 'timeout' => $this->timeout
,
118 // Curl options in case curlwrappers are installed
119 'curl_verify_ssl_host' => $this->sslVerifyHost ?
2 : 0,
120 'curl_verify_ssl_peer' => $this->sslVerifyCert
,
123 'verify_peer' => $this->sslVerifyCert
,
124 'SNI_enabled' => true,
125 'ciphers' => 'HIGH:!SSLv2:!SSLv3:-ADH:-kDH:-kECDH:-DSS',
126 'disable_compression' => true,
130 if ( $this->proxy
) {
131 $options['http']['proxy'] = $this->urlToTcp( $this->proxy
);
132 $options['http']['request_fulluri'] = true;
135 if ( $this->postData
) {
136 $options['http']['content'] = $this->postData
;
139 if ( $this->sslVerifyHost
) {
140 $options['ssl']['peer_name'] = $this->parsedUrl
['host'];
143 $options['ssl'] +
= $this->getCertOptions();
145 $context = stream_context_create( $options );
147 $this->headerList
= [];
153 if ( $this->profiler
) {
154 $profileSection = $this->profiler
->scopedProfileIn(
155 __METHOD__
. '-' . $this->profileName
160 $this->fopenErrors
= [];
161 set_error_handler( [ $this, 'errorHandler' ] );
162 $fh = fopen( $url, "r", false, $context );
163 restore_error_handler();
169 $result = stream_get_meta_data( $fh );
170 $this->headerList
= $result['wrapper_data'];
171 $this->parseHeader();
173 if ( !$this->followRedirects
) {
177 # Handle manual redirection
178 if ( !$this->isRedirect() ||
$reqCount > $this->maxRedirects
) {
181 # Check security of URL
182 $url = $this->getResponseHeader( "Location" );
184 if ( !Http
::isValidURI( $url ) ) {
185 $this->logger
->debug( __METHOD__
. ": insecure redirection\n" );
189 if ( $this->profiler
) {
190 $this->profiler
->scopedProfileOut( $profileSection );
195 if ( $fh === false ) {
196 if ( $this->fopenErrors
) {
197 $this->logger
->warning( __CLASS__
198 . ': error opening connection: {errstr1}', $this->fopenErrors
);
200 $this->status
->fatal( 'http-request-error' );
201 return Status
::wrap( $this->status
); // TODO B/C; move this to callers
204 if ( $result['timed_out'] ) {
205 $this->status
->fatal( 'http-timed-out', $this->url
);
206 return Status
::wrap( $this->status
); // TODO B/C; move this to callers
209 // If everything went OK, or we received some error code
210 // get the response body content.
211 if ( $this->status
->isOK() ||
(int)$this->respStatus
>= 300 ) {
212 while ( !feof( $fh ) ) {
213 $buf = fread( $fh, 8192 );
215 if ( $buf === false ) {
216 $this->status
->fatal( 'http-read-error' );
220 if ( strlen( $buf ) ) {
221 call_user_func( $this->callback
, $fh, $buf );
227 return Status
::wrap( $this->status
); // TODO B/C; move this to callers